<?php

namespace app\data\controller;

use app\data\controller\base\Publics;
use think\Db;
use think\Log;

/**
 * 免申领
 * Class
 * @package app\data\controller
 */
class Apply
{

	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 表
	 * @var  Publics::instance()->tab();
	 */
	protected $tab = null;

	public function __construct()
	{
		$this->tab = Publics::instance()->tab();
	}

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {

			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 免申领权限添加
	 */
	public function store_auth($uid)
	{
		$where = [];
		$where = ["uid" => $uid, "status" => 1];
		$find =	Db::name($this->tab->apply)->where($where)->find();
		if ($find) {
			if ($find["is_apply"] == 2) {
				$update = ["is_apply" => 1, "update_time" => time()];
				Db::name($this->tab->apply)->where($where)->update($update);
			}
		} else {
			$data = ["uid" => $uid, "is_apply" => 1, "create_time" => time()];
			Db::name($this->tab->apply)->insert($data);
		}
	}

	/**
	 * 取消订单修改申领状态
	 */
	public function cancel_update_status($uid, $orid)
	{
		Db::startTrans();
		try {
			$check_apply = $this->check_auth($uid);
			if ($check_apply) {
				$check = $this->check_cords($uid, $orid);
				if ($check) {
					$where = ["uid" => $uid, "order_id" => $orid];
					$ret = Db::name($this->tab->apply_cords)
						->where($where)
						->update(["is_order" => 2]);
					if ($ret) {
						$retUp = Db::name($this->tab->apply)
							->where("uid", $uid)
							->update(["is_apply" => 1, "update_time" => time()]);
						if ($retUp) {
							Db::commit();
						}
					}
				}
			}
		} catch (\Exception $e) {
			Db::rollback();
			Log::write($e->getMessage());
		}
	}

	/**
	 * 下订单修否申领状态
	 */
	public function place_update_status($uid, $orid)
	{
		Db::startTrans();
		try {
			$check_apply = $this->check_auth($uid);
			if ($check_apply) {
				$data = [
					"uid" => $uid,
					"order_id" => $orid
				];
				$check = $this->check_cords($uid, $orid);
				if ($check === false) {
					$data['uid'] = $uid;
					$data['order_id'] = $orid;
					$data['apply_time'] = time();
					$data['create_time'] = time();
					$ret = Db::name($this->tab->apply_cords)->insert($data);
					if ($ret) {
						$retUp = Db::name($this->tab->apply)
							->where("uid", $uid)
							->update(["is_apply" => 2, "update_time" => time()]);
						if ($retUp) {
							Db::commit();
						}
					}
				}
			}
		} catch (\Exception $e) {
			Db::rollback();
			Log::write($e->getMessage());
		}
	}

	/**
	 * 查看 - 申领记录
	 */
	public function check_cords($uid, $orid)
	{
		if ($uid && $orid) {
			$check = Db::name($this->tab->apply_cords)
				->where('uid', $uid)->where("order_id", $orid)
				->count();
			if ($check) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 查看 - 申领权限
	 */
	public function check_auth($uid)
	{
		if ($uid) {
			$check = Db::name($this->tab->apply)
				->where("uid", $uid)
				->count();
			if ($check) {
				return true;
			}
		}
		return false;
	}
}
